const mongoose = require("mongoose");
const QuesSpeed = require("../models/QuesSpeedModel");

exports.addQuesSpeed = async (req, res) => {
  try {
    const newQuesSpeed = await QuesSpeed.create(req.body);
    res.status(201).json({
      status: "success",
      data: newQuesSpeed,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getQuesSpeed = async (req, res) => {
  try {
    const QuesSpeedData = await QuesSpeed.find().sort({ time: -1 });
    res.status(201).json({
      status: "success",
      data: QuesSpeedData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getQuesSpeedByAdress = async (req, res) => {
  const adress = req.params.adress;
  try {
    const QuesSpeedData = await QuesSpeed.findOne({ adress: adress }).sort({
      time: -1,
    });
    res.status(201).json({
      status: "success",
      data: QuesSpeedData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getQuesSpeedByGroup = async (req, res) => {
  const group = req.query.group;
  // console.log(group);
  try {
    const QuesSpeedData = await QuesSpeed.find({ group: group }).sort({
      time: -1,
    });
    res.status(201).json({
      status: "success",
      data: QuesSpeedData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getByAdressAndGroup = async (req, res) => {
  const adress = req.params.adress;
  const group = req.params.group;
  try {
    const QuesSpeedData = await QuesSpeed.find({
      adress: adress,
      group: group,
    }).sort({ time: -1 });
    res.status(201).json({
      status: "success",
      data: QuesSpeedData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.updateQuesSpeed = async (req, res) => {
  try {
    const QuesSpeedData = await QuesSpeed.updateOne(
      { _id: req.body._id },
      req.body
    );
    res.status(201).json({
      status: "success",
      data: QuesSpeedData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};
